﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace Ahorcado
{
    public partial class Teclado : UserControl
    {
        public List<Button> Botones;
        public bool SonidoHabilitado { get; set; }
        public Teclado()
        {
            Botones = new List<Button>();
            InitializeComponent();
            foreach (object obj in this.Controls)
            {
                if (obj is Button)
                {
                    Botones.Add(obj as Button);
                }
            }
            SonidoHabilitado = true;
            Suscribir();
        }
        

        
        public event EventHandler<TecladoEventArgs> TeclaPresionada;

        protected virtual void OnTeclaPresionada(TecladoEventArgs e) {

            if (TeclaPresionada != null)
            {
                TeclaPresionada(this, e);
            }
        }
        public void HabilitarBotones()
        {
            foreach (Button btn in Botones)
            {
                btn.Enabled = true;
            }
        }

        public void Suscribir()
        {
            foreach (Button btn in Botones)
            {
                btn.Click += TeclaPres;
            }
        }
       
        private void TeclaPres(object sender, EventArgs e)
        {
           
            SoundPlayer rep = new SoundPlayer("Sonidos/tick.wav");
            Button boton = sender as Button;
            char caracter = boton.Text[0];
            OnTeclaPresionada(new TecladoEventArgs(caracter));
            if(SonidoHabilitado)
                rep.Play();
            boton.Enabled = false;
        }

        public void Bloquear(string letras)
        {
            foreach (Button btn in Botones)
            {
                if (letras.Contains(btn.Text[0]))
                {
                    btn.Enabled = false;
                }
                else
                    btn.Enabled = true;
                
            }
        }

    }
}
